//
//  NSArray+XXExtension.m
//  SHWheel
//
//  Created by YYKJ on 2020/11/4.
//  Copyright © 2020 YYKJ. All rights reserved.
//

#import "NSArray+XXExtension.h"
#import "NSData+XXExtension.h"

@implementation NSArray (XXExtension)

+ (NSArray *)arrayWithPlistData:(NSData *)plist {
    if (!plist) return nil;
    NSArray *array = [NSPropertyListSerialization propertyListWithData:plist options:NSPropertyListImmutable format:NULL error:NULL];
    if ([array isKindOfClass:[NSArray class]]) return array;
    return nil;
}

+ (NSArray *)arrayWithPlistString:(NSString *)plist {
    if (!plist) return nil;
    NSData* data = [plist dataUsingEncoding:NSUTF8StringEncoding];
    return [self arrayWithPlistData:data];
}

- (NSData *)plistData {
    return [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListBinaryFormat_v1_0 options:kNilOptions error:NULL];
}

- (NSString *)plistString {
    NSData *xmlData = [NSPropertyListSerialization dataWithPropertyList:self format:NSPropertyListXMLFormat_v1_0 options:kNilOptions error:NULL];
    if (xmlData) return xmlData.utf8String;
    return nil;
}

- (id)randomObject {
    if (self.count) {
        return self[arc4random_uniform((u_int32_t)self.count)];
    }
    return nil;
}

- (id)objectOrNilAtIndex:(NSUInteger)index {
    return index < self.count ? self[index] : nil;
}

- (NSString *)jsonStringEncoded {
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:0 error:&error];
        NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return json;
    }
    return nil;
}

- (NSString *)jsonPrettyStringEncoded {
    if ([NSJSONSerialization isValidJSONObject:self]) {
        NSError *error;
        NSData *jsonData = [NSJSONSerialization dataWithJSONObject:self options:NSJSONWritingPrettyPrinted error:&error];
        NSString *json = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
        return json;
    }
    return nil;
}

- (NSArray *)xx_filterWithBlock:(BOOL (NS_NOESCAPE^)(id _Nonnull))block {
    if (!block) { return self; }
    
    NSMutableArray *result = [NSMutableArray array];
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        id item = self[idx];
        if (block(item)) {
            [result addObject:item];
        }
    }];
    
    return result.copy;
}

- (NSArray *)xx_mapWithBlock:(id (NS_NOESCAPE^)(id _Nonnull))block {
    if (!block) { return self; }
    
    NSMutableArray *result = [NSMutableArray arrayWithCapacity:self.count];
    [self enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
        [result addObject: block(self[idx])];
    }];
    
    return result;
}


@end



@implementation NSMutableArray (XXExtension)

- (void)xx_reverse {
    NSUInteger count = self.count;
    int mid = floor(count / 2);
    for (NSUInteger i = 0; i < mid; i++) {
        [self exchangeObjectAtIndex:i withObjectAtIndex:count - (i+1)];
    }
}

- (void)xx_shuffle {
    NSUInteger count = self.count;
    for (NSUInteger i =  count-1; i>=0; i--) {
        [self exchangeObjectAtIndex:(i-1) withObjectAtIndex:arc4random_uniform((UInt32)i)];
    }
}

@end
